<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="google-site-verification" content="yaeorbXgKPwp5BDegBtAJEHYWFEFE-BViWTj74gJPrw" />
<!--  -->
<link href="/css/default.css" type="text/css" rel="stylesheet" />
<!--  -->
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/lang/en.js"></script>
<script type="text/javascript" src="/js/mac/core.min.js"></script>
<title>mac-flow - 进阶</title>
<meta name="description" content="mac-flow 示例" />
<meta name="keywords" content="java 流程引擎 flow" />
<script type="text/javascript">
$(function(){
	function adjust(){
		var de = document.documentElement
			, ch = de.clientHeight, cw = de.clientWidth;
		$('#body').width(cw - 16);
	}
	$(window).resize(adjust);
	adjust();
});
</script>
</head>
<body>
<div id="head">
	<a href="/en/"><img src="/css/images/logo_s.png" alt="logo" /></a>
	<div class="title">BOARSOFT.COM</div>
	<div class="right">
		<div>
			<a href="/public/lang.php?l=en">English</a>
			|
			<a href="/public/lang.php?l=cn">简体中文</a>
			|
			<a href="http://activemq.apache.org/" target="_blank">ActiveMQ</a>
			|
			<a href="https://redis.io/" target="_blank">Redis</a>
			|
			<a href="http://www.jquery.com/" target="_blank">JQuery</a>
		</div>
	</div>
</div><div id="body">
	<div class="dir">
<h1>目录</h1>
<hr/>
<ul>
	<li><a href="/en/mac/flow/index.html">基本概念</a></li>
	<li><a href="/en/mac/flow/config.html">基本配置</a></li>
	<li><a href="/en/mac/flow/demo/index.html#">用法示例（含编辑器用法）</a><li>
	<ul>
		<li><a href="/en/mac/flow/demo/index.html#svc">创建服务</a></li>
		<li><a href="/en/mac/flow/demo/index.html">基本示例</a></li>
		<li><a href="/en/mac/flow/demo/index.html#params">属性配置</a></li>
		<li><a href="/en/mac/flow/demo/index.html#wrapper">包装器</a></li>
		<li><a href="/en/mac/flow/demo/index.html#judge">判定节点</a></li>
		<li><a href="/en/mac/flow/demo/index.html#subflow">流程引用</a></li>
		<li><a href="/en/mac/flow/demo/index.html#while">循环</a></li>
		<li><a href="/en/mac/flow/demo/index.html#fork">并行与异步</a></li>
		<li><a href="/en/mac/flow/demo/index.html#tx">事务处理</a></li>
		<li><a href="/en/mac/flow/demo/index.html#com">创建组件</a></li>
	</ul>
	<li><a href="/en/mac/flow/advance.html">进阶应用</a><li>
	<ul>
		<li><a href="/en/mac/flow/advance.html#persist">持久化</a></li>
		<li><a href="/en/mac/flow/advance.html#xml">自定义生成器</a></li>
		<li><a href="/en/mac/flow/advance.html#threadpool">自定义线程池</a></li>
		<li><a href="/en/mac/flow/advance.html#batch">开发批量</a></li>
		<li><a href="/en/mac/flow/advance.html#mapping">自定义参数映射</a></li>
		<li><a href="/en/mac/flow/advance.html#trace">调用链跟踪</a></li>
	</ul>
	<li><a href="/en/mac/flow/qa.html">Question and Answer</a></li>
</ul>	</div>
	<div class="main">
		<div id="nav">
			<div id="nav_path">
				<a href="/en/">Home
				&gt;
				<a href="/en/mac/flow">mac-flow
				&gt;
				进阶
			</div>
			<div id="nav_back">
				<a href="javascript:history.back()">Back				|
				<a href="/en/">UP</a> &nbsp;&nbsp;
			</div>
			<div class="clear"></div>
		</div>
<h1>mac-flow - 进阶</h1>
<h2><a name="persist"></a>持久化</h2>
&nbsp;&nbsp;当一笔交易开始时，我们通常希望登记流水，并在交易成功完成后更新流水中的交易状态。甚至希望记录交易当时的请求和响应数据。当交易失败需要冲正时，我们还需要将此前保存的数据读取出来，作为相应的反交易的参数。<br/>
&nbsp;&nbsp;如果让应用自己去做，就难以与流程引擎进行整合，两者存在不一致的可能。比如：流程执行失败，而应用的相应的参数和状态更新代码没能得到执行时。<br/>
&nbsp;&nbsp;因此mac-flow提供了流程持久化的接口simpleFlowPerisiter。当流程被创建、发生状态变更时，如：执行完成、失败，会将流程数据和状态写入存储。<br/>
&nbsp;&nbsp;此外，还提供流程的查询、流程数据的读取的接口，以备相应的监控需求使用。
<xmp>
	public interface SimpleFlowPersister {
		/**
		 * 未执行保存动作时返回false<br>
		 * 保存失败则抛出异常
		 * 
		 * @param o
		 * @return
		 */
		boolean save(SimpleFlow o);
	
		/**
		 * 未找到数据时返回null
		 * 
		 * @param id
		 * @return
		 */
		SimpleFlow load(String id);
	
		/**
		 * 未执行保存动作时返回false<br>
		 * 保存失败则抛出异常
		 * 
		 * @param o
		 * @return
		 */
		boolean update(SimpleFlow o);
	
		/**
		 * 1、查询超时未冲正的正交易（无论是否批量冲正的情况）<br>
		 * 2、查询状态为失败且未冲正的正交易（只能是批量冲正的情况）<br>
		 * 3、查询冲正失败但未达到冲正次数上限的正交易
		 * 
		 * @param revMax1
		 *            情形1记录查询上限
		 * @param revMax2
		 *            情形2记录查询上限
		 * @param revMax3
		 *            情形3记录查询上限
		 * @return
		 */
		List<SimpleFlow> list(int revMax1, int revMax2, int revMax3);
	}
</xmp>
&nbsp;&nbsp;为SimpleFlowEngine注入flowPerisister的实现即可实现流程的持久化：flows.xml
<xmp>
	<!-- 可选的流程持久化器 -->
	<bean id="fakeFlowPersister" class="com.boarsoft.flow.demo.store.FakeFlowPersisterImpl"></bean>
	
	<bean id="simpleFlowEngine" class="com.boarsoft.flow.core.SimpleFlowEngineImpl">
		<!-- 用于流程执行所用线程池 -->
		<property name="threadPool" ref="threadPool" />
		<!-- 用于流程超时检查等定时处理的线程池 -->
		<property name="scheduler" ref="scheduler" />
		<!-- 标明当前流程引擎所属的系统（调用链跟踪时用） -->
		<property name="catalog" value="sample" />
		<!-- 此处不注入则不做持久化 -->
		<property name="persister" ref="fakeFlowPersister" />
	</bean>
</xmp>
<h2><a name="xml"></a>自定义生成器</h2>
&nbsp;&nbsp;复制 /mac-flow-editor/www/flow/gen/mac/xml.js<br/>
&nbsp;&nbsp;到 /mac-flow-editor/www/flow/gen/xxx/xml.js<br/>
&nbsp;&nbsp;再修改 mac-flow-editor/www/flow/edit.htm，引用新的xml.js即可
<xmp>
	<script type="text/javascript" src="./flow/gen/mac/xml.js"></script>
</xmp>
<h2><a name="threadpool"></a>自定义线程池</h2>
&nbsp;&nbsp;流程引擎使用的线程池是可配置的，只需要实现ExecutorService即可。线程池的参数和实现对流程引擎的性能有一定影响。
<xmp>
	<bean id="threadPool" class="java.util.concurrent.ThreadPoolExecutor">
		<constructor-arg index="0" value="200" />
		<constructor-arg index="1" value="600" />
		<constructor-arg index="2" value="60000" />
		<constructor-arg index="3">
			<bean class="java.util.concurrent.TimeUnit" factory-method="valueOf">
				<constructor-arg value="MILLISECONDS" />
			</bean>
		</constructor-arg>
		<constructor-arg index="4">
			<bean class="java.util.concurrent.LinkedBlockingQueue">
				<constructor-arg index="0" value="600" />
			</bean>
		</constructor-arg>
		<constructor-arg index="5">
			<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
		</constructor-arg>
	</bean>
</xmp>
<h2><a name="batch"></a>自定义线程池</h2>
&nbsp;&nbsp;某些应用，比如：批量，对线程池有较高要求，需支持任务的取消、优先级调整等等，这就需要编写自已线程池。如有需要请和我联系。
<h2><a name="mapping"></a>自定义参数映射</h2>
&nbsp;&nbsp;当需要较为复杂的服务的出入参转换时，就需要自定义参数映射。这就需要对修改编辑器的相关界面。如有需要请和我联系。
<h2><a name="trace"></a>调用链跟踪</h2>
&nbsp;&nbsp;调用链跟踪基于mac-message消息中间件和mac-log日志中间件实现，专门的log服务用于收集日志，并将日志写入存储，如 MongoDB。如有需要请和我联系。
	</div>
</div>
<div id="foot">
Copyright © 2011. All rights reserved&nbsp;&nbsp;蜀ICP备11014774号-1&nbsp;&nbsp;
QQ: 7213571&nbsp;&nbsp;E-mail: pyh_jerry@163.com&nbsp;&nbsp;
<!-- 
<script type="text/javascript">
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3Ffc0b0282216be3d16055855d8c0e72d9' type='text/javascript'%3E%3C/script%3E"));
</script>
&nbsp;&nbsp;
 -->
</div></body>
</html>
